import 'package:basic_common/app_theme.dart';
import 'package:basic_common/common_exports.dart';
import 'package:basic_services/model/server_fat_detail_data.dart';
import 'package:basic_services/model/server_other_detail_data.dart';
import 'package:basic_services/model/server_press_detail_data.dart';
import 'package:basic_services/model/server_sugar_detail_data.dart';
import 'package:basic_services/model/server_weight_detail_data.dart';
import 'package:basic_widgets/app_bar_widget.dart';
import 'package:basic_widgets/button_widget.dart';
import 'package:basic_widgets/input_widget.dart';
import 'package:basic_widgets/text_widget.dart';
import 'service_detail_controller.dart';
import 'view/add/add_fat_dialog.dart';
import 'view/add/add_other_dialog.dart';
import 'view/add/add_pressure_dialog.dart';
import 'view/add/add_sugar_dialog.dart';
import 'view/add/add_weight_dialog.dart';
import 'view/detail/bloodfat_info_view.dart';
import 'view/detail/other_info_view.dart';
import 'view/detail/press_info_view.dart';
import 'view/detail/sugar_info_view.dart';
import 'view/detail/weight_info_view.dart';

class ServerDetailPage extends GetView<ServerDetailController> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: backAppbar(title: "服务详情"),
      body: SingleChildScrollView(
        physics:const AlwaysScrollableScrollPhysics(),
        child: ListView(
          physics:const NeverScrollableScrollPhysics(),
          shrinkWrap: true,
          children: [
            GetBuilder<ServerDetailController>(
              id: 'serverDetail',
              builder: (controller) => topTipWidget(
                  title:controller.serverStatus.value == '2' ? "已完成" : "等待医生审核中",
                  isComplete: controller.serverStatus.value == '2'),
            ),
            Padding(
              padding: EdgeInsets.symmetric(horizontal: ScreenHelper.width(15)),
              child: norTitle(title: "患者信息"),
            ),
            buildBasicInfo(),
            Padding(
              padding: EdgeInsets.symmetric(horizontal: ScreenHelper.width(15)),
              child: norTitle(title: "服务信息"),
            ),
            buildSignInfo(),
            Obx(() => buildSrDataWidget()),
            Obx(() => Visibility(
              visible: controller.sList.isEmpty,
              child: Container(
                margin: EdgeInsets.symmetric(horizontal: ScreenHelper.width(15)),
                child: btnComponent(
                    onPressed: () {
                      switch (controller.addType.value) {
                        // 血压
                        case "bloodpressure":
                          showAddReplyDialog();
                          break;
                        //血糖
                        case "bloodsugar":
                          showDialog(
                              context: Get.context!,
                              builder: (BuildContext context) {
                                return Container(
                                    padding: EdgeInsets.symmetric(
                                        horizontal: ScreenHelper.width(15)),
                                    child: AddSugarDialog());
                              });
                          break;
                        //体重
                        case "weight":
                          showDialog(
                              context: Get.context!,
                              builder: (BuildContext context) {
                                return Container(
                                    padding: EdgeInsets.symmetric(
                                        horizontal: ScreenHelper.width(15)),
                                    child: AddWeightDialog());
                              });
                          break;
                        //血脂
                        case "bloodfat":
                          controller.fatCellEdits.value = [];
                          for (var element in controller.addFatList) {
                            final textEditingController =
                                TextEditingController(text: element['value']);
                            controller.fatInputControllers
                                .add(textEditingController);
                            controller.fatCellEdits.add(buildAddItem(
                                name: element['name'],
                                unit: element['unit'],
                                inputController: textEditingController));
                          }
                          controller.fatCellEdits.add(buildEstimateWidget());
                          showDialog(
                              context: Get.context!,
                              builder: (BuildContext context) {
                                return Container(
                                    padding: EdgeInsets.symmetric(
                                        horizontal: ScreenHelper.width(15)),
                                    child: AddFatDialog(
                                      fatCellEdits: controller.fatCellEdits,
                                    ));
                              });
                          break;
                        case "other":
                          showDialog(
                              context: Get.context!,
                              builder: (BuildContext context) {
                                return Container(
                                    padding: EdgeInsets.symmetric(
                                        horizontal: ScreenHelper.width(15)),
                                    child: AddOtherDialog());
                              });
                          break;
                      }
                    },
                    textStr: "添加数据",
                    isEnable: true),
              ),
            ))
          ],
        ),
      ),
    );
  }

  /// 显示数据
  Widget buildSrDataWidget() {
    Widget? tempWidget;
    String type = controller.addType.value;
    switch (type) {
      case "other":
        if (controller.resultData.value.isNotEmpty) {
          ServerOtherDetailData detailData =
              ServerOtherDetailData.fromJson(controller.resultData.value);
          if (detailData.sriList!.isNotEmpty) {
            tempWidget = OtherInfoView(detailData: detailData);
          }else{
          tempWidget = btnComponent(
            onPressed: () {
              showDialog(
                  context: Get.context!,
                  builder: (BuildContext context) {
                    return Container(
                        padding: EdgeInsets.symmetric(
                            horizontal: ScreenHelper.width(15)),
                        child: AddOtherDialog());
                  });
            },
            textStr: '添加数据',
            isEnable: true);
          }
        } else {
          tempWidget = SizedBox.shrink();
        }
        break;
      case "bloodsugar":
        ServerSugarDetailData detailData =
            ServerSugarDetailData.fromJson(controller.resultData.value);
        tempWidget = SugarInfoView(
          detailData: detailData,
        );
        break;
      case "bloodfat":
        ServerFatDetailData detailData =
            ServerFatDetailData.fromJson(controller.resultData.value);
        tempWidget = BloodFatInfoView(
          detailData: detailData,
        );
        break;
      case "weight":
       ServerWeightDetailData detailData =
            ServerWeightDetailData.fromJson(controller.resultData.value);
        tempWidget = WeightInfoView(
          detailData: detailData,
        );
        break;
      case "bloodpressure":
        ServerPressDetailData detailData =
            ServerPressDetailData.fromJson(controller.resultData.value);
        tempWidget = PressView(
          detailData: detailData,
        );
        break;
    }
    return tempWidget!;
  }

  /// 添加血糖的数据
  void addSugarDialog() {
    showDialog(
      context: Get.context!,
      builder: (context) {
        return AddSugarDialog();
      },
    );
  }

  /// 添加数据Dialog
  void showAddReplyDialog() {
    controller.cellEdits.value = [];
    controller.addServerList.forEach((element) {
      final textEditingController =
          TextEditingController(text: element['value']);
      controller.inputEditingControllers.add(textEditingController);
      controller.cellEdits.add(buildAddItem(
          name: element['name'],
          unit: element['unit'],
          inputController: textEditingController));
    });
    controller.cellEdits.add(armsSwitch());
    controller.cellEdits.add(buildEstimateWidget());

    showDialog(
        context: Get.context!,
        builder: (BuildContext context) {
          return Container(
            margin: const EdgeInsets.all(10.0),
            child: AddPressureDialog(cellEdits: controller.cellEdits),
          );
        });
  }

// 左臂右臂 选择
  Widget armsSwitch() {
    return Row(
      mainAxisSize: MainAxisSize.min,
      children: [
        SizedBox(
          width: ScreenHelper.width(60),
          child: const Text(
            "手臂",
            textAlign: TextAlign.left,
            style: TextStyle(color: AppTheme.dartWeakTextColor),
          ),
        ),
        Expanded(
          child: Padding(
            padding: EdgeInsets.only(right: ScreenHelper.width(30)),
            child: Obx(() => SizedBox(
                  width: ScreenHelper.screenWidth,
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.start,
                    children: [
                      GestureDetector(
                          onTap: () {
                            controller.armValue.value = '1';
                          },
                          child: buildGenderCheck(
                              title: "左臂",
                              isChecked: controller.armValue.value == '1'
                                  ? true
                                  : false)),
                      SizedBox(
                        width: ScreenHelper.width(ScreenHelper.width(5)),
                      ),
                      Expanded(
                        child: GestureDetector(
                            onTap: () {
                              controller.armValue.value = '2';
                            },
                            child: buildGenderCheck(
                                title: "右臂",
                                isChecked: controller.armValue.value == '2'
                                    ? true
                                    : false)),
                      )
                    ],
                  ),
                )),
          ),
        ),
      ],
    );
  }

  /// 评估内容
  Widget buildEstimateWidget() {
    return Row(
      mainAxisSize: MainAxisSize.min,
      children: [
        SizedBox(
          width: ScreenHelper.width(60),
          child: const Text(
            "评估",
            textAlign: TextAlign.left,
            style: TextStyle(color: AppTheme.dartWeakTextColor),
          ),
        ),
        Expanded(
          child: Container(
            alignment: Alignment.topCenter,
            margin: EdgeInsets.only(top: ScreenHelper.height(10)),
            child: mutileTextField(
                hintText: "请输入评估内容",
                textController: controller.estimateController,
                countNum: "",
                minLines: 2),
          ),
        ),
      ],
    );
  }

  /// item
  Widget buildAddItem(
      {required String name,
      required String unit,
      required TextEditingController inputController}) {
    return SizedBox(
      height: ScreenHelper.height(60),
      child: Row(
        children: [
          SizedBox(
            width: ScreenHelper.width(60),
            child: Text(
              name,
              textAlign: TextAlign.left,
              style: TextStyle(color: AppTheme.dartWeakTextColor),
            ),
          ),
          Expanded(
              child: inputBox(hintText: '', inputController: inputController)),
          SizedBox(
            width: ScreenHelper.width(6),
          ),
          Text(
            '${unit}',
            style: TextStyle(color: AppTheme.dartWeakTextColor),
          ),
        ],
      ),
    );
  }

// 基本信息
  Widget buildBasicInfo() {
    return Container(
        width: ScreenHelper.screenWidth,
        height: ScreenHelper.height(270),
        alignment: Alignment.center,
        padding: EdgeInsets.symmetric(horizontal: ScreenHelper.width(15)),
        margin: EdgeInsets.symmetric(horizontal: ScreenHelper.width(15)),
        child: GetBuilder<ServerDetailController>(
          id: 'serverDetail',
          builder: (controller) => ListView.builder(
            shrinkWrap: true,
            physics: NeverScrollableScrollPhysics(),
            itemBuilder: (context, index) {
              Map item = controller.basicList[index];
              return Container(
                  margin: EdgeInsets.symmetric(vertical: 6),
                  child: textItemWidget(
                      attrName: item['attr'],
                      value: item['value'],
                      fontSize: 16,
                      valuColor: AppTheme.dartTextColor));
            },
            itemCount: controller.basicList.length,
          ),
        ),
        color: Colors.white);
  }

  Widget buildSignInfo() {
    return Container(
        width: ScreenHelper.screenWidth,
        height: ScreenHelper.height(240),
        alignment: Alignment.center,
        padding: EdgeInsets.symmetric(horizontal: ScreenHelper.width(15)),
        margin: EdgeInsets.symmetric(horizontal: ScreenHelper.width(15)),
        child: GetBuilder<ServerDetailController>(
          id: 'serverDetail',
          builder: (controller) => ListView.builder(
            shrinkWrap: true,
            physics: NeverScrollableScrollPhysics(),
            itemBuilder: (context, index) {
              Map item = controller.serverList[index];
              return Container(
                  margin: EdgeInsets.symmetric(vertical: 6),
                  child: textItemWidget(
                      attrName: item['attr'],
                      value: item['value'],
                      fontSize: 16,
                      valuColor: AppTheme.dartTextColor));
            },
            itemCount: controller.serverList.length,
          ),
        ),
        color: Colors.white);
  }
}
